<?php
/**
 * Shortcodes bundled for use with the theme. These shortcodes can be used in any shortcode-ready
 * area, which includes posts, pages, and various Hybrid theme areas.
 *
 * @uses add_shortcode() to create new shortcodes.
 * @link http://codex.wordpress.org/Shortcode_API
 *
 * @package Hybrid
 * @subpackage Functions
 */

/* Add theme-specific shortcodes. */
add_shortcode( 'the-year', 'hybrid_the_year_shortcode' );
add_shortcode( 'site-link', 'hybrid_site_link_shortcode' );
add_shortcode( 'wp-link', 'hybrid_wp_link_shortcode' );
add_shortcode( 'theme-link', 'hybrid_theme_link_shortcode' );
add_shortcode( 'child-link', 'hybrid_child_link_shortcode' );
add_shortcode( 'loginout-link', 'hybrid_loginout_link_shortcode' );
add_shortcode( 'query-counter', 'hybrid_query_counter_shortcode' );

/* Add entry-sepcific shortcodes. */
add_shortcode( 'entry-title', 'hybrid_entry_title_shortcode' );
add_shortcode( 'entry-author', 'hybrid_entry_author_shortcode' );
add_shortcode( 'entry-terms', 'hybrid_entry_terms_shortcode' );
add_shortcode( 'entry-comments-link', 'hybrid_entry_comments_link_shortcode' );
add_shortcode( 'entry-published', 'hybrid_entry_published_shortcode' );
add_shortcode( 'entry-edit-link', 'hybrid_entry_edit_link_shortcode' );

/* Add comment-specific shortcodes. */
add_shortcode( 'comment-published', 'hybrid_comment_published_link_shortcode' );
add_shortcode( 'comment-author', 'hybrid_comment_author' );
add_shortcode( 'comment-edit-link', 'hybrid_comment_edit_link_shortcode' );
add_shortcode( 'comment-reply-link', 'hybrid_comment_reply_link_shortcode' );
add_shortcode( 'comment-permalink', 'hybrid_comment_permalink_shortcode' );

/**
 * Shortcode to display the current year.
 *
 * @since 0.6
 * @uses date() Gets the current year.
 */
function hybrid_the_year_shortcode() {
	return date( __( 'Y', hybrid_get_textdomain()) );
}

/**
 * Shortcode to display a link back to the site.
 *
 * @since 0.6
 * @uses get_bloginfo() Gets information about the install.
 */
function hybrid_site_link_shortcode() {
	return '<a class="site-link" href="' . get_bloginfo( 'url' ) . '" title="' . get_bloginfo( 'name' ) . '" rel="home"><span>' . get_bloginfo( 'name' ) . '</span></a>';
}

/**
 * Shortcode to display a link to WordPress.org.
 * @since 0.6
 */
function hybrid_wp_link_shortcode() {
	return '<a class="wp-link" href="http://wordpress.org" title="' . __( 'Powered by WordPress, state-of-the-art semantic personal publishing platform', hybrid_get_textdomain()) . '"><span>' . __( 'WordPress', hybrid_get_textdomain()) . '</span></a>';
}

/**
 * Shortcode to display a link to the Hybrid theme page.
 *
 * @since 0.6
 * @uses get_theme_data() Gets theme (parent theme) information.
 */
function hybrid_theme_link_shortcode() {
	$data = get_theme_data( TEMPLATEPATH . '/style.css' );
	return '<a class="theme-link" href="' . esc_url( $data['URI'] ) . '" title="' . esc_attr( $data['Name'] ) . '"><span>' . esc_attr( $data['Name'] ) . '</span></a>';
}

/**
 * Shortcode to display a link to the child theme's page.
 *
 * @since 0.6
 * @uses get_theme_data() Gets theme (child theme) information.
 */
function hybrid_child_link_shortcode() {
	$data = get_theme_data( STYLESHEETPATH . '/style.css' );
	return '<a class="child-link" href="' . esc_url( $data['URI'] ) . '" title="' . esc_attr( $data['Name'] ) . '"><span>' . esc_attr( $data['Name'] ) . '</span></a>';
}

/**
 * Shortcode to display a login link or logout link.
 *
 * @since 0.6
 * @uses is_user_logged_in() Checks if the current user is logged into the site.
 * @uses wp_logout_url() Creates a logout URL.
 * @uses wp_login_url() Creates a login URL.
 */
function hybrid_loginout_link_shortcode() {
	$domain = hybrid_get_textdomain();
	if ( is_user_logged_in() )
		$out = '<a class="logout-link" href="' . wp_logout_url( esc_url( $_SERVER['HTTP_REFERER'] ) ) . '" title="' . __( 'Log out of this account', $domain ) . '">' . __( 'Log out', $domain ) . '</a>';
	else
		$out = '<a class="login-link" href="' . wp_login_url( esc_url( $_SERVER['HTTP_REFERER'] ) ) . '" title="' . __( 'Log into this account', $domain ) . '">' . __( 'Log in', $domain ) . '</a>';

	return $out;
}

/**
 * Displays query count and load time if the current user can edit themes.
 *
 * @since 0.6
 * @uses current_user_can() Checks if the current user can edit themes.
 */
function hybrid_query_counter_shortcode() {
	if ( current_user_can( 'edit_themes' ) )
		$out = sprintf( __( 'This page loaded in %1$s seconds with %2$s database queries.', hybrid_get_textdomain()), timer_stop( 0, 3 ), get_num_queries() );
	return $out;
}

/**
 * Displays the edit link for an individual post.
 *
 * @since 0.7
 * @param array $attr
 */
function hybrid_entry_edit_link_shortcode( $attr ) {
	global $post;
	$domain = hybrid_get_textdomain();

	/* @todo Make this compatible with all post types ($capability_type) in WP 3.0. */
	if ( current_user_can( "edit_{$post->post_type}", $post->ID ) || ( is_attachment() && current_user_can( "edit_post", $post->ID ) ) )
		return $attr['before'] . '<span class="edit"><a class="post-edit-link" href="' . get_edit_post_link( $post->ID ) . '" title="' . sprintf( __( 'Edit %1$s', $domain ), $post->post_type ) . '">' . __( 'Edit', $domain ) . '</a></span>' . $attr['after'];

	return '';
}

/**
 * Displays the published date of an individual post.
 *
 * @since 0.7
 * @param array $attr
 */
function hybrid_entry_published_shortcode( $attr ) {
	$domain = hybrid_get_textdomain();
	$attr = shortcode_atts( array( 'format' => get_option( 'date_format' ) ), $attr );

	$published = '<abbr class="published" title="' . sprintf( get_the_time( __( 'l, F jS, Y, g:i a', $domain ) ) ) . '">' . sprintf( get_the_time( $attr['format'] ) ) . '</abbr>';
	return $attr['before'] . $published . $attr['after'];
}

/**
 * Displays a post's number of comments wrapped in a link to the comments area.
 *
 * @since 0.7
 * @param array $attr
 */
function hybrid_entry_comments_link_shortcode( $attr ) {

	$domain = hybrid_get_textdomain();
	$number = get_comments_number();

	if ( $number == 0 )
		$comments_link = '<a class="comments-link" href="' . get_permalink() . '#respond" title="' . sprintf( __( 'Comment on %1$s', $domain ), the_title_attribute( 'echo=0' ) ) . '">' . __( 'Leave a response', $domain ) . '</a>';
	elseif ( $number == 1 )
		$comments_link = '<a class="comments-link" href="' . get_comments_link() . '" title="' . sprintf( __( 'Comment on %1$s', $domain ), the_title_attribute( 'echo=0' ) ) . '">' . __( '1 Response', $domain ) . '</a>';
	else
		$comments_link = '<a class="comments-link" href="' . get_comments_link() . '" title="' . sprintf( __( 'Comment on %1$s', $domain ), the_title_attribute( 'echo=0' ) ) . '">' . sprintf( __( '%1$s Responses', $domain ), $number ) . '</a>';

	return $attr['before'] . $comments_link . $attr['after'];
}

/**
 * Displays an individual post's author with a link to his or her archive.
 *
 * @since 0.7
 * @param array $attr
 */
function hybrid_entry_author_shortcode( $attr ) {
	$author = '<span class="author vcard"><a class="url fn n" href="' . get_author_posts_url( get_the_author_meta( 'ID' ) ) . '" title="' . get_the_author_meta( 'display_name' ) . '">' . get_the_author_meta( 'display_name' ) . '</a></span>';
	return $attr['before'] . $author . $attr['after'];
}

/**
 * Displays a list of terms for a specific taxonomy.
 *
 * @since 0.7
 * @param array $attr
 */
function hybrid_entry_terms_shortcode( $attr ) {
	$attr = shortcode_atts( array( 'taxonomy' => 'post_tag', 'separator' => ', ' ), $attr );

	return '<span class="' . $attr['taxonomy'] . '">' . get_the_term_list( $attr['id'], $attr['taxonomy'], $attr['before'], $attr['separator'], $attr['after'] ) . '</span>';
}

/**
 * Displays a post's title with a link to the post.
 *
 * @since 0.7
 */
function hybrid_entry_title_shortcode() {
	global $post;

	if ( is_front_page() && !is_home() )
		$title = the_title( '<h2 class="page-title entry-title"><a href="' . get_permalink() . '" title="' . the_title_attribute( 'echo=0' ) . '" rel="bookmark">', '</a></h2>', false );

	elseif ( is_singular() )
		$title = the_title( '<h1 class="' . $post->post_type . '-title entry-title"><a href="' . get_permalink() . '" title="' . the_title_attribute( 'echo=0' ) . '" rel="bookmark">', '</a></h1>', false );

	elseif ( 'link_category' == get_query_var( 'taxonomy' ) )
		$title = false;

	else
		$title = the_title( '<h2 class="entry-title"><a href="' . get_permalink() . '" title="' . the_title_attribute( 'echo=0' ) . '" rel="bookmark">', '</a></h2>', false );

	return $title;
}

/**
 * Displays the published date and time of an individual comment.
 *
 * @since 0.7
 */
function hybrid_comment_published_link_shortcode() {
	$domain = hybrid_get_textdomain();
	$link = '<span class="published">' . sprintf( __( '%1$s at %2$s', $domain ), '<abbr class="comment-date" title="' . get_comment_date( __( 'l, F jS, Y, g:i a', $domain ) ) . '">' . get_comment_date() . '</abbr>', '<abbr class="comment-time" title="' . get_comment_date( __( 'l, F jS, Y, g:i a', $domain ) ) . '">' . get_comment_time() . '</abbr>' ) . '</span>';
	return $link;
}

/**
 * Displays the permalink to an individual comment.
 *
 * @since 0.7
 */
function hybrid_comment_permalink_shortcode( $attr ) {
	$domain = hybrid_get_textdomain();
	$link = '<a class="permalink" href="' . get_comment_link( $comment->comment_ID ) . '" title="' . sprintf( __( 'Permalink to comment %1$s', $domain ), $comment->comment_ID ) . '">' . __( 'Permalink', $domain ) . '</a>';
	return $attr['before'] . $link . $attr['after'];
}

/**
 * Displays a comment's edit link to users that have the capability to edit the comment.
 *
 * @since 0.7
 */
function hybrid_comment_edit_link_shortcode( $attr ) {
	global $comment;

	$edit_link = get_edit_comment_link( $comment->comment_ID );

	if ( !$edit_link )
		return '';
	$domain = hybrid_get_textdomain();

	$link = '<a class="comment-edit-link" href="' . $edit_link . '" title="' . __( 'Edit comment', $domain ) . '"><span class="edit">' . __( 'Edit', $domain ) . '</span></a>';
	$link = apply_filters( 'edit_comment_link', $link, $comment->comment_ID );

	return $attr['before'] . $link . $attr['after'];
}

/**
 * Displays a reply link for the 'comment' comment_type if threaded comments are enabled.
 *
 * @since 0.7
 */
function hybrid_comment_reply_link_shortcode( $attr ) {
	$domain = hybrid_get_textdomain();

	if ( !get_option( 'thread_comments' ) || 'comment' !== get_comment_type() )
		return '';

	$defaults = array(
		'reply_text' => __( 'Reply', $domain ),
		'login_text' => __( 'Log in to reply.', $domain ),
		'depth' => $GLOBALS['comment_depth'],
		'max_depth' => get_option( 'thread_comments_depth' ),
		'before' => '',
		'after' => ''
	);
	$attr = shortcode_atts( $defaults, $attr );

	return get_comment_reply_link( $attr );
}

?>